package sren.actor

import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.{ActorContext, Behaviors}
import com.almasb.fxgl.animation.{Animation, AnimationBuilder, Interpolators}
import javafx.geometry
import javafx.scene.shape.Circle
import scalafx.scene.layout.*
import scalafx.scene.control.*
import org.slf4j.{Logger, LoggerFactory}
import scalafx.collections.ObservableBuffer
import scalafx.geometry.Point2D
import scalafx.scene.shape.{Line, QuadCurve}
import scalafx.util.Duration


object MainActor {

  def apply(): Behavior[String] = {
    Behaviors.setup(ctx => new MainActor(ctx).init)
  }
}

class MainActor(ctx: ActorContext[String]) {

  val log: Logger = LoggerFactory.getLogger(this.getClass)

  val mainUI = new FxglMain().start(Array.empty[String])

  val flp = ctx.spawn(FiveLinePlayer(), "flp")

  def init = Behaviors.receiveMessage[String] { msg =>
    log.info(s"start Main actor. ${mainUI}, ${flp}")
    Behaviors.same
  }
}

import com.almasb.fxgl.app.GameApplication
class FxglMain extends GameApplication {
  import com.almasb.fxgl.app.GameApplication.launch
  import com.almasb.fxgl.app.{GameApplication, GameSettings}
  import com.almasb.fxgl.app.GameApplication
  import com.almasb.fxgl.app.GameSettings
  import com.almasb.fxgl.dsl.FXGL
  import com.almasb.fxgl.entity.Entity
  import com.almasb.fxgl.input.Input
  import com.almasb.fxgl.input.UserAction
  import javafx.scene.image.Image
  import javafx.scene.input.KeyCode
  import javafx.scene.paint.Color
  import javafx.scene.shape.Rectangle
  import javafx.scene.text.Text
  override def initSettings(settings: GameSettings): Unit = {
    settings.setTitle("drum learn")
    settings.setHeight(800)
    settings.setWidth(1280)
  }

  override def initUI(): Unit = {
    import scalafx.scene.control.*
    FXGL.getGameScene.getHeight
    FXGL.getGameScene.addUINodes(
      new BorderPane() {
        translateX = 0
        translateY = 500
        top = new VBox {
          children = Seq(
            new ListView[String]() {
              prefHeight = 200
              maxHeight = 200
              items = ObservableBuffer("music1", "music2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2", "2")
            }
          )
        }
        bottom = new ToolBar() {
          items = Seq(
            new Button("asdf") {},
            new Button("asdf") {},
            new Button("asdf") {}
          )
        }
        right = new VBox {
          children = Seq(
            new ToolBar {
              items = Seq(
                new Button("<<") { prefWidth=60},
                new Button(">") { prefWidth=60},
                new Button("T") { prefWidth=60},
                new Button(">>") { prefWidth=60},
              )
            },
            new ToolBar {
              items = Seq(
                new Button("A") { prefWidth=60},
                new Button("repeat") { prefWidth=120},
                new Button("B") { prefWidth=60},
              )
            }
          )
        }
      }
    )
  }

  var player: Entity = null
  var pointer: Entity = null
  val startPoint = new Point2D(100, 60)
  val endPoint = new Point2D(1100, 60)

  extension (p : Point2D) {
    def addx(dx: Double): Point2D = new Point2D(p.x + dx, p.y)
    def addy(dy: Double): Point2D = new Point2D(p.x, p.y + dy)
    def add(dx: Double, dy: Double) = new Point2D(p.x+dx, p.y+dy)
  }

  override def initGame(): Unit = {
    FXGL.entityBuilder()
      .at(50, 50)
      .view("5line.jpg")
      .scale(1.4, 1)
      .buildAndAttach()
    player = FXGL.entityBuilder()
      .at(startPoint)
      .view("gb.png")
      .scale(0.2, 0.2)
      .buildAndAttach();
    pointer = FXGL.entityBuilder()
      .at(startPoint.addx(6))
      .view(new Circle(6, Color.ORANGERED))
      .buildAndAttach();

    FXGL.animationBuilder()
      .duration(Duration(4000))
      .repeatInfinitely()
      .translate(player)
      .from(startPoint)
      .to(endPoint)
      .buildAndPlay();

    val shape = Line(0,0, 50,50)

    import javafx.scene.shape.Polyline
    val polyline = new Polyline(0, 0, 20, 10, 10, 20)

    FXGL.animationBuilder()
      .duration(Duration(4000))
      .repeatInfinitely()
      .translate(pointer)
      .alongPath(polyline)
      .buildAndPlay()
  }

  override def initInput(): Unit = {
    FXGL.onKey(KeyCode.A,  ()=> {
      player.translateX(-5d)
    })
    FXGL.onKey(KeyCode.W,  ()=> {
      player.translateY(-5d)
    })
    FXGL.onKey(KeyCode.D,  ()=> {
      player.translateX(5d)
    })
    FXGL.onKey(KeyCode.S,  ()=> {
      player.translateY(5d)
    })
  }


  def start(args: Array[String]): Unit = {
    launch(args)
  }
}